package com.example.takeaway.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.takeaway.entity.User;
import com.example.takeaway.serivce.IUserService;
import com.example.takeaway.utils.PageResult;
import com.example.takeaway.utils.PassToken;
import com.example.takeaway.utils.ServerResponse;
import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * userController
 * 
 * @author ruoyi
 * @date 2025-05-16
 */
@RestController
@RequestMapping("/system/user")
public class UserController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询user列表
     */
    @PassToken
    @GetMapping("/list")
    public ServerResponse list(User user ,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {

        PageResult pageResult = userService.selectUserList(user,pageNum,pageSize);
        return  ServerResponse.createBysuccess(200,pageResult,"查询成功");
    }

    /**
     * 导出user列表
     */
//    @PreAuthorize("@ss.hasPermi('system:user:export')")
//    @Log(title = "user", businessType = BusinessType.EXPORT)


    /**
     * 获取user详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{userid}")
    public ServerResponse getInfo(@PathVariable("userid") Long userid)
    {
       User user = userService.selectUserByUserid(userid);
       return ServerResponse.createBysuccess(200,user,"查询成功");
    }

    /**
     * 新增user
     */
//    @PreAuthorize("@ss.hasPermi('system:user:add')")
//    @Log(title = "user", businessType = BusinessType.INSERT)
    @PostMapping
    public ServerResponse add(@RequestBody User user){
        int i = userService.insertUser(user);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }


    }

    /**
     * 修改user
     */
//    @PreAuthorize("@ss.hasPermi('system:user:edit')")
//    @Log(title = "user", businessType = BusinessType.UPDATE)
    @PutMapping
    public ServerResponse edit(@RequestBody User user)
    {
        int i =userService.updateUser(user);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }

    /**
     * 删除user
//     */
//    @PreAuthorize("@ss.hasPermi('system:user:remove')")
//    @Log(title = "user", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userids}")
    public ServerResponse remove(@PathVariable Long[] userids)
    {
        int i = userService.deleteUserByUserids(userids);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }
}
